package com.yjs.ppjokedemo;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yjs.ppjokedemo.adapter.FraAdapter;
import com.yjs.ppjokedemo.bean.CustomEntity;
import com.yjs.ppjokedemo.ui.FindFragment;
import com.yjs.ppjokedemo.ui.HomeFragment;
import com.yjs.ppjokedemo.ui.MineFragment;
import com.yjs.ppjokedemo.ui.PublishFragment;
import com.yjs.ppjokedemo.ui.SofaFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private CommonTabLayout tab;

    FraAdapter mFraAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //解决白屏启动问题 优化
        setTheme(R.style.Theme_AppCompat_Light_NoActionBar);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        tab = (CommonTabLayout) findViewById(R.id.tab);

        ArrayList<CustomTabEntity> tabs = new ArrayList<>();
        tabs.add(new CustomEntity("首页",R.mipmap.icon_tab_home,R.mipmap.icon_tab_home));
        tabs.add(new CustomEntity("沙发",R.mipmap.icon_tab_sofa,R.mipmap.icon_tab_sofa));
        tabs.add(new CustomEntity("",R.mipmap.icon_tab_publish,R.mipmap.icon_tab_publish));
        tabs.add(new CustomEntity("发现",R.mipmap.icon_tab_find,R.mipmap.icon_tab_find));
        tabs.add(new CustomEntity("我的",R.mipmap.icon_tab_mine,R.mipmap.icon_tab_mine));
        tab.setTabData(tabs);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new SofaFragment());
        fragments.add(new PublishFragment());
        fragments.add(new FindFragment());
        fragments.add(new MineFragment());
        mFraAdapter = new FraAdapter(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragments);
        vp.setAdapter(mFraAdapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tab.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });



    }
}